package com.imooc.user.controller;

import com.imooc.api.controller.user.MyFansControllerApi;
import com.imooc.enums.Sex;
import com.imooc.grance.result.GraceJsonResult;
import com.imooc.grance.result.ResponseStatusEnum;
import com.imooc.pojo.vo.FansCountsVO;
import com.imooc.pojo.vo.RegionRatioVO;
import com.imooc.user.service.MyFansService;
import com.imooc.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MyFansController implements MyFansControllerApi {

    @Autowired
    private MyFansService myFansService;

    @Override
    public GraceJsonResult isMeFollerThisWriter(String fanId, String writerId) {
        if (StringUtils.isBlank(fanId) || StringUtils.isBlank(writerId)) {
            return GraceJsonResult.error();
        }
        boolean meFollowThisWriter = myFansService.isMeFollowThisWriter(fanId, writerId);
        return GraceJsonResult.ok(meFollowThisWriter);
    }

    @Override
    public GraceJsonResult follow(String writerId, String fanId) {
        if (StringUtils.isBlank(writerId) || StringUtils.isBlank(fanId)) {
            return GraceJsonResult.error();
        }
        myFansService.follow(writerId, fanId);
        return GraceJsonResult.ok();
    }

    @Override
    public GraceJsonResult unfollow(String writerId, String fanId) {
        if (StringUtils.isBlank(writerId) || StringUtils.isBlank(fanId)) {
            return GraceJsonResult.error();
        }
        myFansService.unfollow(writerId, fanId);
        return GraceJsonResult.ok();
    }

    @Override
    public GraceJsonResult queryAll(String writerId,
                                    Integer page,
                                    Integer pageSize) {
        PagedGridResult pagedGridResult = myFansService.queryAll(writerId, page, pageSize);

        return GraceJsonResult.ok(pagedGridResult);
    }

    @Override
    public GraceJsonResult queryRatio(String writerId) {
        Integer manFans = myFansService.queryCounts(writerId, Sex.man);
        Integer womanFans = myFansService.queryCounts(writerId, Sex.woman);
        FansCountsVO fansCountsVO = new FansCountsVO();
        fansCountsVO.setManCounts(manFans);
        fansCountsVO.setWomanCounts(womanFans);
        System.out.println(fansCountsVO);
        return GraceJsonResult.ok(fansCountsVO);
    }

    @Override
    public GraceJsonResult queryRatioByRegion(String writerId) {
        List<RegionRatioVO> list = myFansService.queryRatioByRegion(writerId);
        System.out.println(list.toString());
        return GraceJsonResult.ok(list);
    }

}
